﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Drawing.Drawing2D; 
using System.Windows.Forms;
//using System.Threading;

namespace ArchiveSystem.ShowBusy
{
    public partial class BusyForm : Form
    {
        private Point mouse_offset;
        public MyThread myThread;
        Busy _b;

        public BusyForm(Busy b)
        {
            _b = b;
            InitializeComponent();
        }

        int num = 0;
        int second = 0;
        bool count = true;

        private void timer_Tick(object sender, EventArgs e)
        {
 
            try
            {
                if (_b != null)
                {
                    if (_b.operate == "hide")
                        this.Hide();
                    if (_b.operate == "close")
                        this.Close();
                }

                if (count)
                {
                    if (num >= ilPics.Images.Count)
                        num = 0;
                    plPic.BackgroundImage = ilPics.Images[num];
                    num++;

                    if (second == 10)
                    {
                        lbSec.Text = ((int)(int.Parse(lbSec.Text) + 1)).ToString();
                        lbSec.Location = new Point(plPic.Width / 2 - lbSec.Width / 2, lbSec.Location.Y);
                        lbSec.Visible = true;
                        second = 0;
                    }
                    else
                        second++;
                }
            }
            catch
            {
                this.Close();
            }
        }
     
        private void BusyForm_Load(object sender, EventArgs e)
        {
            if (myThread != null)
                plThread.Visible=true;
           
            if (progressBar.Maximum == 0)
            {
                this.Width = 107;
                this.Height = 107;
            }

            //SetWindowRegion();
        }

        //绘制窗体圆角
        public void SetWindowRegion()
        {
            System.Drawing.Drawing2D.GraphicsPath FormPath;
            FormPath = new System.Drawing.Drawing2D.GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);//this.Left-10,this.Top-10,this.Width-10,this.Height-10);                 
            FormPath = GetRoundedRectPath(rect, 30);
            this.Region = new Region(FormPath);
        }
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角   
            path.AddArc(arcRect, 180, 90);
            //   右上角   
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角   
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角   
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);

            path.CloseFigure();
            return path;
        }

        delegate void SetProgressCallback(int maximun);
        public void SetProgressBarMaximum(int maximun)
        {
            try
            {
                if (progressBar.InvokeRequired)
                {
                    SetProgressCallback spc = new SetProgressCallback(SetProgressBarMaximum);
                    this.Invoke(spc, new object[] { maximun });
                }
                else
                {
                    progressBar.Maximum = maximun;

                    if (progressBar.Maximum == 0)
                    {
                        this.Width = 107;
                        this.Height = 107;
                        lbPercent.Visible = progressBar.Visible = false;
                    }
                    else
                    {
                        this.Width = 130;
                        this.Height = 130;
                        lbPercent.Visible=progressBar.Visible = true;
                    }
                }
            }
            catch
            {
                this.Close();
            }
        }

        delegate void AddProgressBarValueCallback(int StepValue);
        public void AddProgressBarValue(int StepValue)
        {
            try
            {
                if (progressBar.InvokeRequired)
                {
                    AddProgressBarValueCallback spc = new AddProgressBarValueCallback(AddProgressBarValue);
                    this.Invoke(spc, new object[] { StepValue });
                }
                else
                {
                    if (progressBar.Value + StepValue > progressBar.Maximum)
                        return;
                    progressBar.Value = progressBar.Value + StepValue;
                    int percent = (int)(((float)progressBar.Value / (float)progressBar.Maximum) * 100);
                    lbPercent.Text = percent.ToString() + "%";
                }
            }
            catch
            {
                this.Close();
            }
        }

        public void SetProgressBarValue(int _value)
        {
            progressBar.Value = _value;
        }
        private void plPic_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
        }

        private void plPic_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                Location = mousePos;
            }
        }

        private void plPic_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
            if (_b != null)
                _b.Stop();
            else
                this.Close();
        }

        public void CloseForm()
        {
           // closed = true;
            this.Dispose();

        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (myThread.Started && !myThread.Suspended)
            {
                myThread.Suspend();
                count = false;
                btnPause.Image = global::ShowBusy.Properties.Resources.start;
            }

            else if (myThread.Started && myThread.Suspended)
            {
                myThread.Resume();
                count = true;
                btnPause.Image = global::ShowBusy.Properties.Resources.pause;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                if (myThread.Started)
                  _b.Stop();
            }
            catch
            {
                return;
            }
        }

        private void btnPause_MouseEnter(object sender, EventArgs e)
        {
            btnPause.BackColor = Color.Black;
            Point p = new Point(btnPause.Location.X + btnPause.Width, btnPause.Location.Y + btnPause.Height);
            if (myThread.Started && myThread.Suspended)
                toolTip.Show("继续当前操作...", this, this.PointToClient(plThread.PointToScreen(p)));
            else if (myThread.Started && !myThread.Suspended)
                toolTip.Show("暂停当前操作...", this, this.PointToClient(plThread.PointToScreen(p)));
        }

        private void btnPause_MouseLeave(object sender, EventArgs e)
        {
            btnPause.BackColor = Color.Transparent;
            toolTip.Hide(this);
           
        }

        private void btnStop_MouseEnter(object sender, EventArgs e)
        {
            btnStop.BackColor = Color.Black;
            Point p = new Point(btnStop.Location.X + btnStop.Width, btnStop.Location.Y + btnStop.Height);
            toolTip.Show("停止当前操作...", this, this.PointToClient(plThread.PointToScreen(p)));
        }

        private void btnStop_MouseLeave(object sender, EventArgs e)
        {
            btnStop.BackColor = Color.Transparent;
            toolTip.Hide(this);
            //btnStop.Image = global::ArchiveSystem.ShowBusy.Properties.Resources.stop1;
        }

        delegate void CommunionCallback(string communion);
        public void CallCommunion(string communion)
        {
            if (this.InvokeRequired)
            {
                CommunionCallback ccb = new CommunionCallback(CallCommunion);
                this.Invoke(ccb, new object[] { communion });
                return;
            }

            this.Width =250;
            this.Height = 215;
            this.Opacity = 100;
            gbCommunion.Text = communion;
            gbCommunion.Visible = true;

            btnPause_Click(null, null);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定进行当前操作？", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                this.DialogResult = DialogResult.Cancel;
            }

            if (txtCommunion.Text.Trim() == "")
            {
                MessageBox.Show("请输入"+gbCommunion.Text+"！", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                return;
            }
            _b.Communion = txtCommunion.Text.Trim();
            gbCommunion.Visible = false;

            this.Width = 130;
            this.Height = 130;
            this.Opacity = 80;
            btnPause_Click(null, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnStop_Click(null, null);
        }

      
    }
}